<template>
  <div class="header">
    <div class="logo">后台管理系统</div>
    <div class="user-info">
      <el-dropdown trigger="hover" @command="handleCommand">
                <span class="el-dropdown-link">
                    <img class="user-logo" src="static/img/logo.png">
                    {{username}}
                </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>我的消息</el-dropdown-item>
          <el-dropdown-item command="updatePassword">修改密码</el-dropdown-item>
          <el-dropdown-item command="loginout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        userName: ''
      }
    },
    created(){
      if(sessionStorage.getItem('myUserName')){
        this.userName=sessionStorage.getItem('myUserName');
      }else {
        this.$router.push('/login');
      }
    },
    computed: {
      username() {
        let username = sessionStorage.getItem('myUserName');
        return username ? username : this.name;
      }
    },
    methods: {
      handleCommand(command) {
        if (command == 'loginout') {
          this.$confirm('确定退出登录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            sessionStorage.removeItem('myUserName');
            sessionStorage.removeItem('myUserId');
            sessionStorage.removeItem('myAccessToken');
            this.$message({
              type: 'success',
              message: '退出成功!'
            });
            this.$router.push('/login');
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消退出'
            });
          });
        } else if (command == 'updatePassword') {
          this.$router.push('/modifyPassword');
        }
      }
    }
  }
</script>
<style scoped>
  .header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    line-height: 70px;
    color: #fff;
    background-color: #242f42;;
  }

  .header .logo {
    float: left;
    width: 250px;
    text-align: center;
  }

  .user-info {
    float: right;
    padding-right: 50px;
    font-size: 16px;
    color: #fff;
  }

  .user-info .el-dropdown-link {
    position: relative;
    display: inline-block;
    padding-left: 50px;
    color: #fff;
    cursor: pointer;
    vertical-align: middle;
  }

  .user-info .user-logo {
    position: absolute;
    left: 0;
    top: 15px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }

  .el-dropdown-menu__item {
    text-align: center;
  }
</style>
